﻿Imports MySQL
Imports MySQLCommand
Imports System.Data.SqlClient
Imports System.Configuration

Public Class frmNCC
    Sub Connect()
        clsSQL.setConnection(ConfigurationSettings.AppSettings("connect"))
        If clsSQL.Connected() = False Then
            MsgBox("Lỗi kết nối")
            'MsgBox("Kết nối thành công")
        End If
    End Sub
    Private Sub frmNCC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connect()
        loadNCC()
    End Sub

    Sub loadNCC()
        DsNhaCungCap.Tables("sp_NCC_Select").Clear()
        clsSQL.FillDataSet("sp_NCC_Select", DsNhaCungCap, "sp_NCC_Select")
    End Sub
    Sub Macdinh()
        txtTenNCC.Text = ""
        txtDiachiNCC.Text = ""
        txtDienthoaiNCC.Text = ""
        txtFaxNCC.Text = ""
        mancc = 0
        btnThemNCC.BackgroundImage = ASIA.My.Resources.Resources.add
    End Sub

    Private Sub btnHuyNCC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyNCC.Click
        Macdinh()
    End Sub
    Dim mancc As Integer = 0
    Private Sub btnThemNCC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNCC.Click

        If mancc = 0 Then 'add NCC
            If txtTenNCC.Text <> "" Then
                clsSQL.ExecReader("sp_InsertUpdateDeleteNCC", "i", mancc, txtTenNCC.Text, txtDiachiNCC.Text, txtDienthoaiNCC.Text, txtFaxNCC.Text)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                Macdinh()
                loadNCC()
            End If
        Else 'update NCC
            If txtTenNCC.Text <> "" Then
                clsSQL.ExecReader("sp_InsertUpdateDeleteNCC", "u", mancc, txtTenNCC.Text, txtDiachiNCC.Text, txtDienthoaiNCC.Text, txtFaxNCC.Text)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                Macdinh()
                loadNCC()
            End If
        End If
    End Sub

   
    Private Sub dtgNCC_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgNCC.DoubleClick
        mancc = dtgNCC.Item(0, dtgNCC.CurrentRow.Index).Value
        txtTenNCC.Text = dtgNCC.Item(1, dtgNCC.CurrentRow.Index).Value.ToString
        txtDiachiNCC.Text = dtgNCC.Item(2, dtgNCC.CurrentRow.Index).Value.ToString
        txtDienthoaiNCC.Text = dtgNCC.Item(3, dtgNCC.CurrentRow.Index).Value.ToString
        txtFaxNCC.Text = dtgNCC.Item(4, dtgNCC.CurrentRow.Index).Value.ToString
        btnThemNCC.BackgroundImage = ASIA.My.Resources.Resources.save
    End Sub

    Private Sub mniXoaNCC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniXoaNCC.Click
        mancc = dtgNCC.Item(0, dtgNCC.CurrentRow.Index).Value
        clsSQL.ExecReader("sp_InsertUpdateDeleteNCC", "d", mancc, txtTenNCC.Text, txtDiachiNCC.Text, txtDienthoaiNCC.Text, txtFaxNCC.Text)
        clsSQL.getData.Read()
        MsgBox(clsSQL.getData("Err"))
        Macdinh()
        loadNCC()
    End Sub

    Private Sub mnuNCC_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mnuNCC.Opening
        If dtgNCC.Rows.Count <> 0 Then
            mniXoaNCC.Enabled = True
        Else
            mniXoaNCC.Enabled = False

        End If
    End Sub
End Class